package com.tzxs.pay.main.onlineaut.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.BottomSheetDialog;
import android.support.design.widget.Snackbar;
import android.text.InputFilter;
import android.text.TextUtils;
import android.view.View;
import android.widget.CalendarView;
import android.widget.Toast;

import com.tzxs.pay.R;
import com.tzxs.pay.databinding.FragmentOnlineAutBinding;
import com.tzxs.pay.main.BaseFragment;
import com.tzxs.pay.main.onlineaut.AmountFilter;
import com.tzxs.pay.main.onlineaut.OnlineUseInfoActivity;
import com.tzxs.pay.main.onlineaut.SendMsgActivity;
import com.tzxs.pay.main.onlineaut.broadcast.AutFinishBroadCast;
import com.tzxs.pay.main.onlineaut.navigator.FinishNavigator;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * 在线预授权
 */
public class OnAutFragment extends BaseFragment<FragmentOnlineAutBinding> {


    @Override
    public int getContentLayoutId() {
        return R.layout.fragment_online_aut;
    }

    @Override
    protected void viewCreate(Bundle savedInstanceState) {

        binding.all.setFilters(new InputFilter[]{new AmountFilter()});

        //使用说明
        binding.useInfo.setOnClickListener(v -> {

            Intent autOnlineIntent = new Intent(getActivity(), OnlineUseInfoActivity.class);
            startActivity(autOnlineIntent);
        });
        //编辑短信
        binding.editStart.setOnClickListener(v -> {

            if (TextUtils.isEmpty(binding.inPeople.getText().toString())) {
                Snackbar.make(binding.editStart, "请输入入住人", Snackbar.LENGTH_SHORT).show();
                return;
            }
            if (TextUtils.isEmpty(binding.phoneNumber.getText().toString())) {
                Snackbar.make(binding.editStart, "请输入手机号", Snackbar.LENGTH_SHORT).show();
                return;
            }
            if (TextUtils.isEmpty(binding.inDate.getText().toString())) {
                Snackbar.make(binding.editStart, "请选择入住日期", Snackbar.LENGTH_SHORT).show();
                return;
            }

            if (TextUtils.isEmpty(binding.note.getText().toString())) {
                Snackbar.make(binding.editStart, "请输入备注", Snackbar.LENGTH_SHORT).show();
                return;
            }
            if (TextUtils.isEmpty(binding.all.getText().toString())) {
                Snackbar.make(binding.editStart, "请输入金额", Snackbar.LENGTH_SHORT).show();
                return;
            }

            if (timeJudge(binding.inDate.getText().toString()) < System.currentTimeMillis()) {
                Toast.makeText(getContext(), "入住日期不能小于当前时间", Toast.LENGTH_SHORT).show();
                return;
            }
            //跳转到编辑短信页面
            Intent intent = new Intent(getContext(), SendMsgActivity.class);
            intent.putExtra("inPeople", binding.inPeople.getText().toString());
            intent.putExtra("phone", binding.phoneNumber.getText().toString());
            intent.putExtra("time", binding.inDate.getText().toString());
            intent.putExtra("note", binding.note.getText().toString());
            intent.putExtra("amount", binding.all.getText().toString());
            startActivity(intent);


        });

        //点击选择时间
        binding.inDate.setOnClickListener(v -> chooseTime());


    }

    /**
     * 选择时间
     */
    private void chooseTime() {
        if (getContext() == null) {
            return;
        }
        final BottomSheetDialog bottomSheetDialog = new BottomSheetDialog(getContext());
        View content = getLayoutInflater().inflate(R.layout.time_choose, null);
        CalendarView calendarView = content.findViewById(R.id.order_time_calendarView);
        bottomSheetDialog.setContentView(content);
        bottomSheetDialog.show();
        calendarView.setOnDateChangeListener((view, year, month, dayOfMonth) -> {
            bottomSheetDialog.dismiss();
            String mM;
            String day;
            if (dayOfMonth < 10) {//日
                day = "0" + String.valueOf(dayOfMonth);
            } else {
                day = String.valueOf(dayOfMonth);
            }

            if (month < 10) {
                mM = "0" + String.valueOf(month + 1);
            } else {
                mM = String.valueOf(month + 1);
            }
            String time = year + "-" + mM + "-" + day;
            binding.inDate.setText(time);


        });
    }

    //判断时间段选择
    private long timeJudge(String time) {
        SimpleDateFormat simpleDateFormat = (SimpleDateFormat) SimpleDateFormat.getDateInstance();
        simpleDateFormat.applyPattern("yyyy-MM-dd");
        try {
            Date date = simpleDateFormat.parse(time);
            return date.getTime();
        } catch (ParseException e) {
            e.printStackTrace();
        }

        return 0;
    }

    public void clear() {
        binding.all.setText("");
        binding.note.setText("");
        binding.phoneNumber.setText("");
        binding.inPeople.setText("");
        binding.inDate.setText("");
    }
}
